package cn.itcast.travel.service.impl;

import cn.itcast.travel.domain.blog;
import cn.itcast.travel.domain.blogger;
import cn.itcast.travel.domain.blogtype;
import cn.itcast.travel.domain.comment;
import cn.itcast.travel.service.AdminService;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //获取所有用户
    @Override
    public List<blogger> getalluser() {
        List<blogger> bs=null;
        String sql="select * from t_blogger";
        bs= template.query(sql,new BeanPropertyRowMapper<blogger>(blogger.class));
        return bs;
    }

    //删除用户
    @Override
    public void deleteuser(String id) {
        String sql1="delete from t_collection where bloggerid=?;";
        String sql2="delete from t_comment where bloggerid=?;";
        String sql3="delete from t_mood where bloggerid=?;";
        String sql4="delete from t_blog where bloggerid=?;";
        String sql5="delete from t_blogger where id=?;";
        template.update(sql1,id);
        template.update(sql2,id);
        template.update(sql3,id);
        template.update(sql4,id);
        template.update(sql5,id);
    }

    //修改用户
    @Override
    public void xiugai(blogger br) {
        String sql="update t_blogger set userName=?,password=?,nickName=?,aventer=? where id=?";
        template.update(sql,br.getUserName(),br.getPassword(),br.getNickName(),br.getAventer(),br.getId());
    }

    //修改用户权限先
    @Override
    public void xiugairight(String which,String id) {
        if(which.equals("1")){
            String sql="update t_blogger set pubblog=not pubblog where id=?";
            template.update(sql,id);
        }else{
            String sql="update t_blogger set pubcom=not pubcom where id=?";
            template.update(sql,id);
        }
    }

    //获取博客以及博客的评论
    @Override
    public List<blog> getblogcom() {
        List<blog> bs=null;
        String sql="select * from t_blog";
        bs=template.query(sql,new BeanPropertyRowMapper<blog>(blog.class));
        for(blog b:bs){
            int blogid=b.getId();
            String sql1="select * from t_comment where blogid=?";
            List<comment> cs=template.query(sql1,new BeanPropertyRowMapper<comment>(comment.class),blogid);
            b.setComs(cs);
        }
        return bs;
    }

    //修改博客
    @Override
    public void xiugaiblog(blog b) {
        String sql="update t_blog set title=?,summary=?,pic=? where id=?";
        template.update(sql,b.getTitle(),b.getSummary(),b.getPic(),b.getId());
    }

    //删除博客
    @Override
    public void deleteblog(String id) {
        String sql1="delete from t_collection where bgid=?;";
        String sql2="delete from t_comment where blogid=?;";
        String sql3="delete from t_blog where id=?;";
        template.update(sql1,id);
        template.update(sql2,id);
        template.update(sql3,id);
    }

    //删除评论
    @Override
    public void deletecom(String id) {
        String sql="delete from t_comment where id=?";
        template.update(sql,id);
    }

    //修改评论
    @Override
    public void xiugaipin(comment c) {
        String sql="update t_comment set content=? where id=?;";
        template.update(sql,c.getContent(),c.getId());
    }

    //删除博客类别
    @Override
    public void deleteblogtype(String id) {
        String sql1="select * from t_blog where typeId=?;";
        List<blog> bs=template.query(sql1,new BeanPropertyRowMapper<blog>(blog.class),id);
        for(blog b:bs){
            String sql2="delete from t_comment where blogid=?;";
            String sql3="delete from t_collection where bgid=?;";
            String sql4="delete from t_blog where id=?";
            template.update(sql2,b.getId());
            template.update(sql3,b.getId());
            template.update(sql4,b.getId());
        }
        String sql5="delete from t_blogtype where id=?";
        template.update(sql5,id);
    }

    //修改博客类别
    @Override
    public void xiugaitype(blogtype bt) {
        String sql="update t_blogtype set typeName=? where id=?";
        template.update(sql,bt.getTypeName(),bt.getId());
    }

    //添加博客类别
    @Override
    public void addtype(String typeName) {
        String sql="insert into t_blogtype(typeName) value(?)";
        template.update(sql,typeName);
    }
}
